﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace EigenFaces {
	public partial class TouchControls : UserControl {
		private static string Scan = "&Scannen";
		private static string Accept = "&Bestätigen";
		private static string Correct = "&Korrigieren";
		private static string Cancel = "&Abbrechen";
		private static string Add = "&Hinzufügen";
		private static string Unknown = "Unbekannt";
		private static string Scanning = "bitte in die Kamera sehen";
		private static string Working = "Erkennung läuft...";

		public event ClassNameEventHandler ClassNameAccepted;

		public TouchControls() {
			InitializeComponent();
			DoScan = false;
			SetStartState();
		}

		public bool DoScan { get; set; }

		public float Timeout {
			get { return Convert.ToSingle(timeOutTimer.Interval) / 1000; }
			set { timeOutTimer.Interval = Convert.ToInt32(value * 1000); }
		}

		private void SetStartState() {
			SetTexts(Scan, null, "", false);
			buttonLeft.RemoveClickEvent();
			buttonLeft.Click += OnDoScan;
		}

		private void SetWaitState() {
			SetTexts(null, Cancel, Scanning, false);
			buttonRight.RemoveClickEvent();
			buttonRight.Click += OnCancel;
		}

		public void ScanFinished(string className) {
			if( DoScan ) {
				timeOutTimer.Enabled = false;
				DoScan = false;
				if( className == null ) {
					SetNotFoundState();
				} else {
					SetFoundState(className);
				}
			}
		}

		public void SetWorkingState() {
			textBox.Text = Working;
		}

		private void SetNotFoundState() {
			SetTexts(Add, Cancel, Unknown, true);
			buttonLeft.RemoveClickEvent();
			buttonRight.RemoveClickEvent();
			buttonLeft.Click += OnAccept;
			buttonRight.Click += OnCancel;
		}

		private void SetFoundState(string className) {
			SetTexts(Accept, Correct, className, false);
			buttonLeft.RemoveClickEvent();
			buttonRight.RemoveClickEvent();
			buttonLeft.Click += OnAccept;
			buttonRight.Click += OnChange;
		}

		private void SetEditState() {
			SetTexts(Accept, Cancel, null, true);
			buttonLeft.RemoveClickEvent();
			buttonRight.RemoveClickEvent();
			buttonLeft.Click += OnAccept;
			buttonRight.Click += OnCancel;
			textBox.Select();
			textBox.SelectAll();
		}


		private void OnAccept(object sender, EventArgs e) {
			ClassNameAccepted(this, new ClassNameEventArgs(textBox.Text));
			SetStartState();
		}

		private void OnChange(object sender, EventArgs e) {
			SetEditState();
		}


		private void OnDoScan(object sender, EventArgs e) {
			DoScan = true;
			timeOutTimer.Enabled = true;
			SetWaitState();
		}

		private void OnCancel(object sender, EventArgs e) {
			timeOutTimer.Enabled = false;
			DoScan = false;
			SetStartState();
		}

		private void SetTexts(string textB1, string textB2, string textT1, bool writeT1) {
			buttonLeft.Visible = textB1 != null;
			buttonLeft.Text = textB1;

			buttonRight.Visible = textB2 != null;
			buttonRight.Text = textB2;

			textBox.ReadOnly = !writeT1;

			if( textT1 != null )
				textBox.Text = textT1;

			if( writeT1 ) {
				textBox.ForeColor = Color.Black;
				textBox.BackColor = Color.White;
			} else {
				textBox.ForeColor = Color.White;
				textBox.BackColor = Color.Black;
			}
		}


		public void SetNameList(Dictionary<string, List<DotNetMatrix.Matrix>> dictionary) {
			textBox.AutoCompleteCustomSource.Clear();
			foreach( var pair in dictionary ) {
				textBox.AutoCompleteCustomSource.Add(pair.Key);
			}
		}

		private void OnTextKeyDown(object sender, KeyEventArgs e) {
			if( e.KeyData == Keys.Enter ) {
				OnAccept(sender, new EventArgs());
			}
		}
	}

	public delegate void ClassNameEventHandler(object sender, ClassNameEventArgs args);

	public class ClassNameEventArgs : EventArgs {
		public string ClassName { get; private set; }

		public ClassNameEventArgs(string className) {
			ClassName = className;
		}
	}
}
